# -*- coding:utf-8 -*-
# @FileName  :t16.py
# @Time      :2021/7/15 16:03
import base64
import io
import sys
import time
from Crypto.Util.Padding import pad
import execjs
import requests
from Crypto.Cipher import AES
from binascii import b2a_hex

sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='gb18030')


def add_to_16(text):
    if len(text.encode('utf-8')) % 16:
        add = 16 - (len(text.encode('utf-8')) % 16)
    else:
        add = 0
    text = text + ('\0' * add)
    return text.encode('utf-8')


# 加密函数
def encrypt(text, m):
    key = m.encode('utf-8')
    mode = AES.MODE_ECB
    text = add_to_16(text)
    cryptos = AES.new(key, mode)
    cipher_text = cryptos.encrypt(text)
    return b2a_hex(cipher_text)


def jie_m(i):
    with open('t5_m.js', 'r', encoding='gbk') as f:
        jstext = f.read()
    ctx = execjs.compile(jstext)
    result = ctx.call('_0x474032', i)
    return result


# def jie_f(i):
#     with open('m_cookie.js', 'r', encoding='gbk') as f:
#         jstext = f.read()
#     ctx = execjs.compile(jstext)
#     result = ctx.call('get_m_cookie', i)
#     return result


def main(data, m, f, page):
    global num
    kv = {'User-Agent': 'yuanrenxue.project', 'cookie': f'sessionid=tm5itokq2gl88fw1m9rwmtctkox003pv;RM4hZBv0dDon443M={data};m=' + jie_m(m)}
    par = {
        'page': page,
        'm': m,
        'f': f,
    }
    r = requests.get(f'https://match.yuanrenxue.com/api/match/5', headers=kv, params=par).json()
    print(r)
    for i in r['data']:
        num.append(i['value'])


if __name__ == "__main__":
    # print(jie_m('1626770221786'))
    # print(jie_f('1626770221786'))
    num = []
    for i in range(1, 6):
        mw = []
        f = str(int(time.time())) + '000'
        m = str(int(time.time() * 1000))
        mw.append(jie_m(f))
        for j in range(3):
            k = str(int(time.time())) + '000'
            time.sleep(0.2)
            mw.append(jie_m(k))
        mw.append(jie_m(m))
        ok = ','.join(mw)
        key = base64.b64encode(m[:-1].encode())
        cryptor = AES.new(key=key, mode=AES.MODE_ECB)
        data = base64.b64encode(cryptor.encrypt(pad(ok.encode(), AES.block_size))).decode()
        main(data, m, f, i)
    num.sort(reverse=True)
    l = 0
    for i in num[:5]:
        l += i
    print(l)
'''
5zJuywiFBHa93Qe30bzGCO6IwXlBhLVZMqN6tjfqemlmkxL9unvJ3tgpOzjJ7QwW47hZxq8+lyZ9VqqXiI16+s8QAFF2Y8ApaM6ue3cYnQtJZE+5bgK+TNd6qQxnlORi7PYI8T6wp0y9b+4NzkWcZHPQYRApA4GrkgRoDH1NR9k0ErZ61xi5fHnwC9vMjxgjpXpFaXLP2YAw0fZUgbFGc8kikNAKgoHb+YFyMyMAh44=
明文：e489a858ff23c11faef5a9a22da29704,c5838e043aedcc16b75f49cf1e338b2f,49dc1c797af45bec19af3572d11c4ef2,24d00c7abc024537e639a7a1f97cffbe,039f2d238f14e25b15fe1eb3ae9e95f9
1626770221786
1626770221000
密钥:通过时间戳base64加密 截取16位数再转换16进制作为密钥
window['_$qF'] = CryptoJS['enc']['Utf8']['parse'](window['btoa'](window['_$is'])['slice'](0, 16));
加密的明文为16进制的5个时间点的时间戳第一个为 f 后面3个比f大即可  最后一个为 m
AES ECB 填充为pkcs7padding  加密
'''
